package cn.stylefeng.guns.sys.modular.metadataCol.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.metadataCol.entity.MetadataCol;
import cn.stylefeng.guns.sys.modular.metadataCol.mapper.MetadataColMapper;
import cn.stylefeng.guns.sys.modular.metadataCol.model.params.MetadataColParam;
import cn.stylefeng.guns.sys.modular.metadataCol.model.result.MetadataColResult;
import cn.stylefeng.guns.sys.modular.metadataCol.service.MetadataColService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 数据元表 服务实现类
 * </p>
 *
 * @author author
 * @since 2020-11-19
 */
@Service
public class MetadataColServiceImpl extends ServiceImpl<MetadataColMapper, MetadataCol> implements MetadataColService {

    @Override
    public void add(MetadataColParam param) {
        MetadataCol entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MetadataColParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(MetadataColParam param) {
        MetadataCol oldEntity = getOldEntity(param);
        MetadataCol newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MetadataColResult findBySpec(MetadataColParam param) {
        return null;
    }

    @Override
    public List<MetadataColResult> findListBySpec(MetadataColParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MetadataColParam param) {
        param.setIsDelete(1);
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public int deleteMetadataDetailInfo(String id) {
        return this.baseMapper.deleteMetadataDetail(id);
    }

    private Serializable getKey(MetadataColParam param) {
        return param.getMetadataColId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MetadataCol getOldEntity(MetadataColParam param) {
        return this.getById(getKey(param));
    }

    private MetadataCol getEntity(MetadataColParam param) {
        MetadataCol entity = new MetadataCol();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
